/*
 * 
 * @author		: Anand Sivadas
 * @version		: 0.1
 * @Date		: Jul 21, 2014
 * 
 */
package com.sivadas.anand.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sivadas.anand.dao.EmployeeDAO;
import com.sivadas.anand.entity.EmployeeEntity;

/**
 * The Class EmployeeDAOImpl.
 *
 * @author 	: Anand Sivadas
 * @Date 	: Jul 21, 2014
 */
@Repository
public class EmployeeDAOImpl implements EmployeeDAO {
	
	Logger LOGGER = LoggerFactory.getLogger(EmployeeDAOImpl.class);

	@Autowired
	private SessionFactory sessionFactory;

	/**
	 * Gets the session factory.
	 *
	 * @return the session factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Sets the session factory.
	 *
	 * @param sessionFactory the new session factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/* (non-Javadoc)
	 * @see com.sivadas.anand.dao.EmployeeDAO#addEmployee(com.sivadas.anand.entity.EmployeeEntity)
	 */
	public void addEmployee(EmployeeEntity employee) {
		
		this.sessionFactory.getCurrentSession().save(employee);
	}

	/* (non-Javadoc)
	 * @see com.sivadas.anand.dao.EmployeeDAO#getAllEmployees()
	 */
	@SuppressWarnings("unchecked")
	public List<EmployeeEntity> getAllEmployees() {
		
		List<EmployeeEntity> employeeList = this.sessionFactory.getCurrentSession()
		.createQuery("from EmployeeEntity").list();
		
		LOGGER.info("employeeList.sizr() = " + employeeList); 
		
		for (EmployeeEntity employeeEntity : employeeList) {
			LOGGER.info(employeeEntity.toString());
		}
		
		return employeeList;
	}

	/* (non-Javadoc)
	 * @see com.sivadas.anand.dao.EmployeeDAO#deleteEmployee(java.lang.Integer)
	 */
	public void deleteEmployee(Integer employeeId) {
		EmployeeEntity employee = (EmployeeEntity) sessionFactory
				.getCurrentSession().load(EmployeeEntity.class, employeeId);
		if (null != employee) {
			this.sessionFactory.getCurrentSession().delete(employee);
		}
	}

}
